package cn.ffcs.uec.outStaff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.manager.UecBdDefdocManager;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.common.vo.UecBdDefdoc;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffHiPsnWorkEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.post.vo.BasePostVo;
import cn.ffcs.uec.staff.model.UecWork;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecOutStaffHiPsnWorkEditComposer extends BasePortletComposer {

    private static final long serialVersionUID = 1904355814960068165L;

    private UecOutStaffHiPsnWorkEditBean bean = new UecOutStaffHiPsnWorkEditBean();

    /**
     * 操作类型
     */
    private String opType;
/**
 * 选择岗位体系（新、旧）
 */
    private int selectType=0;
    /**
     * 人员信息
     */
    private UecOutStaffInfo uecOutStaffInfo;

    /**
     * 履历记录
     */
    private UecWork uecWork;
    /**
     * 履历记录(临时表)
     */
    private UecWork tempUecWork;
    private UecBdDefdocManager uecBdDefdocManager = (UecBdDefdocManager) ApplicationContextUtil.getBean("uecBdDefdocManager");
    private UecBdDefdoc uecBdDefdoc;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
       // this.bean.getOldPostLevel().addForward("onOldPostLevel", comp,
        //		"onGetOldPostLevelResponse");
        this.bean.getSelectRadio().addForward("onCheck", comp,
        		"onGetOldPostLevelResponse");
    }

    /**
     * 初始化窗口
     */
    public void onCreate$uecOutStaffHiPsnWorkEditWindow() {
        this.bindCombobox();
        this.bindBean();
    }

    /**
     * bindBean
     */
    public void bindBean() {
        opType = (String) arg.get("opType");
        if ("add".equals(opType)) {
            this.bean.getUecOutStaffHiPsnWorkEditWindow().setTitle("新增人员履历记录");
            bean.getUecOutStaffHiPsnWorkEditDiv().setHeight("30%");
            uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
            bean.getOldPostLevel().setVisible(false);
            bean.getOldPostLevelLabel().setVisible(false);
        }
        if ("edit".equals(opType)) {
            this.bean.getUecOutStaffHiPsnWorkEditWindow().setTitle("编辑人员履历记录");
            bean.getUecOutStaffHiPsnWorkEditDiv().setHeight("30%");
            uecWork = (UecWork) arg.get("uecWork");
            tempUecWork=uecWork;
            if (null != uecWork) {
            	uecBdDefdoc= new UecBdDefdoc();
            	if(!StrUtil.isNullOrEmpty(uecWork.getGlbdef1())) {
            		uecBdDefdoc.setPkDefdoc(uecWork.getGlbdef1());
            		uecBdDefdoc = uecBdDefdocManager.qryUecBdDefdoc(uecBdDefdoc);
            	}
            	PubUtil.fillBeanFromPo(uecWork, bean);
                bean.getPostGrade().setValue(uecWork.getGlbdef2Name());
                if("1001A1100000000005KE".equals(uecBdDefdoc.getPid()) || "1001A1100000000005KF".equals(uecBdDefdoc.getPid())) {
                	bean.getNewPostLevel().setValue(uecWork.getGlbdef1Name());
                	bean.getNewPostLevel().setVisible(true);
        			bean.getOldPostLevel().setVisible(false);
        			bean.getNewPostLevelLabel().setVisible(true);
        			bean.getOldPostLevelLabel().setVisible(false);
        			selectType=0;
        			bean.getSelectRadio().getItemAtIndex(1).setChecked(false);
        			bean.getSelectRadio().getItemAtIndex(0).setChecked(true);
                	
                }
                if("10000000000000000000".equals(uecBdDefdoc.getPid())) {
                	bean.getOldPostLevel().setValue(uecWork.getGlbdef1Name());
                	bean.getNewPostLevel().setVisible(false);
        			bean.getOldPostLevel().setVisible(true);
        			bean.getNewPostLevelLabel().setVisible(false);
        			bean.getOldPostLevelLabel().setVisible(true);
        			selectType=1;
        			bean.getSelectRadio().getItemAtIndex(1).setChecked(true);
        			bean.getSelectRadio().getItemAtIndex(0).setChecked(false);
                }
            }
        }
    }
	/**
	 * 监听岗位层级体系(新、旧)
	 * 
	 * @param event
	 */
	public void onGetOldPostLevelResponse(final ForwardEvent event) {
		selectType=bean.getSelectRadio().getSelectedIndex();
		if(selectType==0) {
			bean.getNewPostLevel().setVisible(true);
			bean.getOldPostLevel().setVisible(false);
			bean.getOldPostLevel().setValue(null);
			bean.getNewPostLevelLabel().setVisible(true);
			bean.getOldPostLevelLabel().setVisible(false);
		}else {
			bean.getNewPostLevel().setVisible(false);
			bean.getOldPostLevel().setVisible(true);
			bean.getNewPostLevel().setValue(null);
			bean.getNewPostLevelLabel().setVisible(false);
			bean.getOldPostLevelLabel().setVisible(true);
		}
	}
    /**
     * 保存
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        String msg = this.checkUacStaffData();
        if (null != msg) {
            Messagebox.show(msg);
            return;
        }
        // 获取登录用户
        String currentName = "";
        if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
            ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
            return;
        } else {
            currentName = PlatformUtil.getCurrentUser().getScreenName();
        }
        if ("add".equals(opType)) {
            uecWork = UecWork.newInstance();
            PubUtil.fillPoFromBean(bean, uecWork);
            dateTransToString();
            uecWork.setStaffId(uecOutStaffInfo.getOutStaffId());
            uecWork.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
            uecWork.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);//外包数据
            //判断新、旧岗位层级是否都选择，规定只能选择一个
            if(selectType==0) {
	            if(bean.getNewPostLevel().getUecBdDefdocs()!=null) {
	            		uecWork.setGlbdef1(bean.getNewPostLevel().getUecBdDefdocs().get(0).getPkDefdoc());
	            }
            }else {
	            if(bean.getOldPostLevel().getUecBdDefdocs()!=null) {
	            		uecWork.setGlbdef1(bean.getOldPostLevel().getUecBdDefdocs().get(0).getPkDefdoc());
	            }
            }
            uecWork.addOnly();
            Events.postEvent("onOK", this.self, uecWork);
            this.onCancel();
            Messagebox.show("新增成功！");
        }
        if ("edit".equals(opType)) {
            PubUtil.fillPoFromBean(bean, uecWork);
            dateTransToString();
            if(bean.getPostGrade().getUecBdDefdocs()!=null) {
            String postGrade = bean.getPostGrade().getUecBdDefdocs().get(0).getName();
            String postGradeOld = tempUecWork.getGlbdef2Name();
	            if(!postGrade.equals(postGradeOld)) {
	            	uecWork.setGlbdef2(bean.getPostGrade().getUecBdDefdocs().get(0).getPkDefdoc());
	            }else {
	            	uecWork.setGlbdef2(tempUecWork.getGlbdef2());
	            }
            }else {
            	uecWork.setGlbdef2(tempUecWork.getGlbdef2());
            }
            
            //判断新、旧岗位层级是否都选择，规定只能选择一个
           
            if(selectType==0) {
            }else {
            }
            if(selectType==0) { 
	            if(bean.getNewPostLevel().getUecBdDefdocs()!=null && bean.getOldPostLevel().getUecBdDefdocs()==null) {
	            	String postLevel = bean.getNewPostLevel().getUecBdDefdocs().get(0).getName();
	            	String postLevelOld = tempUecWork.getGlbdef1Name();
	            	if(!postLevelOld.equals(postLevel)) {
	            		uecWork.setGlbdef1(bean.getNewPostLevel().getUecBdDefdocs().get(0).getPkDefdoc());
	            	}else {
	            		uecWork.setGlbdef1(tempUecWork.getGlbdef1());
	            	}
	            }
            }else {
	            if(bean.getOldPostLevel().getUecBdDefdocs()!=null) {
	            	String postLevel = bean.getOldPostLevel().getUecBdDefdocs().get(0).getName();
	            	String postLevelOld = tempUecWork.getGlbdef1Name();
	            	if(!postLevelOld.equals(postLevel)) {
	            		uecWork.setGlbdef1(bean.getOldPostLevel().getUecBdDefdocs().get(0).getPkDefdoc());
	            	}else {
	            		uecWork.setGlbdef1(tempUecWork.getGlbdef1());
	            	}
	            }
            }
            if(bean.getNewPostLevel().getUecBdDefdocs()==null && bean.getOldPostLevel().getUecBdDefdocs()==null) {
            	uecWork.setGlbdef1(tempUecWork.getGlbdef1());
            }
            uecWork.setModifier(currentName);
            uecWork.setModifiedtime(DateUtil.getCurrenDate());
            uecWork.updateOnly();
            Events.postEvent("onOK", this.self, uecWork);
            this.onCancel();
            Messagebox.show("更新成功！");
        }
    }

    /**
     * 取消
     */
    public void onCancel() {
        // 关闭窗口
        this.bean.getUecOutStaffHiPsnWorkEditWindow().onClose();
    }

    /**
     * 检测填入的参数是否为空
     */
    private String checkUacStaffData() {
        if (StrUtil.isNullOrEmpty(bean.getWorkpost().getValue())) {
            return "请填写岗位";
        } else if (StrUtil.isNullOrEmpty(bean.getWorkjob().getValue())) {
            return "请填写职务";
        } else if (StrUtil.isNullOrEmpty(bean.getWorkcorp().getValue())) {
            return "请填写工作单位";
        } else if (StrUtil.isNullOrEmpty(bean.getWorkdept().getValue())) {
            return "请填写部门";
        }
        return null;
    }

    /**
     * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
     */
    public void dateTransToString() {
        if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
            // 开始日期
            uecWork.setBegindate(DateUtil.dateToStr(bean.getBegindate().getValue()));
        }
        if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
            // 结束日期
            uecWork.setEnddate(DateUtil.dateToStr(bean.getEnddate().getValue()));
        }
    }

    /**
     * 绑定combobox
     */
    private void bindCombobox() {
        // 岗位层级--glbdef1
        ListboxUtils.rendererForEdit(bean.getGlbdef1(), NodeVoUtil.getDictionaries(UecConstants.POST_HIERARCHY));
        // 岗位等级--glbdef2
        ListboxUtils.rendererForEdit(bean.getGlbdef2(), NodeVoUtil.getDictionaries(UecConstants.POST_LEVEL));
        // 岗位层级_ZY --glbdef3
        ListboxUtils.rendererForEdit(bean.getGlbdef3(), NodeVoUtil.getDictionaries(UecConstants.POST_HIERARCHY));
        // 岗位等级_zy -- glbdef4
        ListboxUtils.rendererForEdit(bean.getGlbdef4(), NodeVoUtil.getDictionaries(UecConstants.POST_HIERARCHY));
    }
}
